package common;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import connection.IServerConnection;
import connection.ServerConnectionConfig;

public class EasyChatEnvironment {

	private static EasyChatEnvironment easyChatEnvironment = new EasyChatEnvironment();
	private static long version = 0;

	public static EasyChatEnvironment getInstance() {
		return easyChatEnvironment;
	}

	public static long getVersion() {
		return version;
	}
	public static void setVersion(long version) {
		EasyChatEnvironment.version = version;
	}
	private List<BodySet> listBodies;

	private List<Discussion> discussions;

	private List<IServerConnection> connecionList;

	public EasyChatEnvironment() {
		setListBodies(new ArrayList<BodySet>());
		setDiscussions(new ArrayList<Discussion>());
		setConnecionList(new ArrayList<IServerConnection>());
	}

	public void addDiscussion(Discussion discussion) {
		for (Discussion idiscussion : getDiscussions()) {
			if (idiscussion.equals(discussion)) {
				idiscussion.setState(discussion.getState());
				return;
			}
		}
		discussions.add(discussion);
	}

	public void addServerConnection(IServerConnection connection) throws EasyTalkException {
	
		if (findServer(connection.getConfig())!=null)
			throw new EasyTalkException(Messages.getString("EasyChatEnvironment.0")); //$NON-NLS-1$
		BodySet set = createBodySet();
		set.setConnection(connection);
		try {
			connection.hookBodySet(set);
		} catch (Exception e) {
			e.printStackTrace();
			listBodies.remove(0);
			return;
		} finally {
			connecionList.add(connection);
			version++;
		}
	}

	public IServerConnection findServer(ServerConnectionConfig config) {
		for (IServerConnection connection : connecionList) {
			if (connection.getConfig().equals(config))
				return connection;
		}
		return null;
	}

	public BodySet createBodySet() {
		BodySet bSet = new BodySet(this);
		listBodies.add(0, bSet);
		return bSet;
	}

	public Discussion findDiscussionById(String id) {
		for (Discussion discussion : discussions) {
			if (discussion.getId()!=null)
			if (discussion.getId().equals(id))
				return discussion;
		}
	return null;
	}
	
	public Discussion createDiscussion(String id) {
		Discussion discussion = new Discussion(this);
		discussion.setId(id);
		return discussion;
	}

	public Discussion findDiscussion(String locutorID) {
		for (Discussion discussion : getDiscussions()) {
			if (discussion.getLocutors().get(0).getId().equals(locutorID)) {
				return discussion;
			}
		}
		return null;
	}

	public List<Discussion> FindDiscussionsWith(Locutor locutor) {
		List<Discussion> ret = new ArrayList<Discussion>();
		for (Discussion discussion : getDiscussions()) {
			if (discussion.contains(locutor))
				ret.add(discussion);
		}
		return ret;
	}

	public List<Locutor> getAllLocutors() {
		List<Locutor> ret = new ArrayList<Locutor>();
		for (BodySet bodySet : listBodies) {
			ret.addAll(bodySet.getLocutors());
		}
		return ret;
	}

	public List<IServerConnection> getConnecionList() {
		return connecionList;
	}

	public List<Discussion> getDiscussions() {
		return discussions;
	}

	public List<BodySet> getListBodies() {
		return listBodies;
	}

/*	public Locutor getLocutorById(String Id) {
		for (Locutor locutor : getAllLocutors()) {
			if (locutor.getId().equals(Id))
				return locutor;
		}
		return null;
	}
*/
	public Locutor getLocutorById(String id, Comparator<String> comparator) throws EasyTalkException {
		for (Locutor locutor : getAllLocutors()) {
			
			if (comparator.compare(id, locutor.getId()) ==0) {
				return locutor;
			}
		}
		for (BodySet set : getListBodies()) {
			if  (comparator.compare(set.getMe().getId(),id)==0)
				return set.getMe();
		}
		
		throw new EasyTalkException("locutor not found: "+id);
	}

	public void setConnecionList(List<IServerConnection> connecionList) {
		this.connecionList = connecionList;
	}

	public void setDiscussions(List<Discussion> discussions) {
		this.discussions = discussions;
	}

	public void setListBodies(List<BodySet> listBodies) {
		this.listBodies = listBodies;
	}

	public void update() {
		version++;
	}

}
